<?php

/**
 * gestion des tag des publications
 *
 * @author Gmax
 */
class modele_tag extends modele_category {
    public $type = 'tag';
    protected $_discriminant = 'tag';
    
    public function __construct($id = null) {
        parent::__construct($id);
    }
    
    /**
     * ajoute une publication au tag
     * @param int $publication_id
     * @param int $order
     */
    public function addPublication($publication_id=0,$order=0){
        if(!self::checkTagPublication($this->getId(), $publication_id)){
            $sqlQuery = 'INSERT INTO `'.PWE::getPrefixBdd().'category_publication` ';
            $sqlQuery .= '(category_id,publication_id,`order`) ';
            $sqlQuery .= 'VALUES ('.$this->getId().','.$publication_id.','.$order.')';
            bdd_sql::doSql($sqlQuery,false);
        }
        return true;
    }
    
    /**
     * suprime une publication du tag
     * @param int $publication_id
     * @return boolean
     */
    public function removePublication($publication_id=0){
        $sqlQuery = 'DELETE FROM `'.PWE::getPrefixBdd().'category_publication` WHERE category_id='.$this->getId().' AND publication_id='.$publication_id;
        bdd_sql::doSql($sqlQuery,false);
        return true;
    }
    
    /**
     * renvoi le nombre d'articles rattachés à ce tag
     * @param boolean $public
     * @return int
     */
    public function getNombrePublications($public=false,$startDate=0,$endDate=0){
        return count($this->getPublications(0,0,$public));
    }
    
    /**
     * renvoi la liste des article attachés à ce tag
     * @param int $start
     * @param int $limit
     * @param boolean $public
     * @param boolean $desc
     * @return \modele_publication
     */
    public function getPublications($start = 0, $limit = 0, $public = false, $desc=true,$startDate=0,$endDate=0){
        $retour = array();
        $sqlQuery = 'SELECT cp.publication_id AS publication_id FROM `'.PWE::getPrefixBdd().'category_publication` AS cp';
        $sqlQuery .= ' LEFT JOIN '.modele_publication::getTable().' AS p ON p.id=cp.publication_id';
        if($public){
            $sqlQuery .= ' AND p.statut_id=2';
        }
        
        $sqlQuery .= ' WHERE cp.category_id='.$this->getId();
        
        $sqlQuery .= ' ORDER BY cp.`order`, p.`date`';
        $sqlQuery .= ($desc===true)?'DESC':'ASC';
        if($limit>0){
            $sqlQuery .= ' LIMIT '.$start.','.$limit.' ';
        }
        
        $datas = $this->_getResults($sqlQuery);
        if($datas){
            foreach( $datas['results'] as $publication ){
                $retour[] = new modele_publication($publication['publication_id']);
            }
        }
        
        return $retour;
    }
    
    public function delete() {
        // hierarchie
        if($this->haveChild()){
            foreach ($this->getChild() as $child){
                $child->delete();
            }
        }
                
        // entree en bdd
        $sqlDelete = 'DELETE FROM '.self::getTable().' WHERE id='.$this->getId();
        bdd_sql::doSql($sqlDelete,false);
        
        //permalien
        $this->getPermalien()->delete();
        return array('success'=>true,'datas'=>array());
    }
    /* methodes statiques */
    
    /**
     * vérifie l'existence d'une relation publication<->tag
     * @param int $tag_id
     * @param int $publication_id
     * @return boolean
     */
    public static function checkTagPublication($tag_id=0,$publication_id=0){
        $return = false;
        $sqlQuery = 'SELECT * FROM `'.PWE::getPrefixBdd().'category_publication` WHERE category_id='.$tag_id.' AND publication_id='.$publication_id;
        $results = bdd_sql::doSql($sqlQuery,false);
        if($results['count']>0){
            $return = true;
        }
        return $return;
    }
    /**
     * renvoi la liste des tag
     * @param type $start
     * @return \modele_tag
     */
    public static function getPrimaryListe($start=0, $limit=0){
        $sqlQuery = 'SELECT id FROM '.self::getTable().' WHERE parent_id IS NULL AND discriminant="tag" ORDER BY order_idx,id DESC';
        if($limit>0){
            $sqlQuery .=  ' LIMIT '.$start.', '.$limit;
        }
        $retour = array();
        $results = bdd_sql::doSql($sqlQuery,true);
        if($results['count']>0){
            foreach($results['results'] as $categoryData){
                $retour[] = new modele_tag($categoryData['id']);
            }
        }
        return $retour;
    }
    public static function getListe($start = 0, $limit=0) {
        return self::getPrimaryListe($start,$limit);
    }
    
    /**
     * renvoi la liste des tag associés à une publication
     * @param int $publication_id
     * @param int $start
     * @param int $limit
     * @param boolean $public
     * @return \modele_tag
     */
    public static function getListeByPublication($publication_id=0,$start=0,$limit=0,$public=false){
        $retour = array();
        $sqlQuery = 'SELECT * FROM `'.PWE::getPrefixBdd().'category_publication` AS cp ';
        $sqlQuery .= 'LEFT JOIN '.self::getTable().' AS c ON c.id=cp.category_id ';
        $sqlQuery .= 'WHERE cp.publication_id='.$publication_id.' AND c.discriminant="tag" ';
        if($public){
            $sqlQuery .= ' AND c.statut_id=2';
        }
        $sqlQuery .=  ' ORDER BY cp.`order`';
        if($limit>0){
            $sqlQuery .= ' LIMIT '.$start.', '.$limit;
        }
        $results = bdd_sql::doSql($sqlQuery,true);
        if($results['count']>0){
            foreach($results['results'] as $datas){
                $retour[] = new modele_tag($datas['category_id']);
            }
        }
        
        return $retour;
    }
}
